package firear.hooksms;

import android.app.PendingIntent;
import android.util.Log;

import com.saurik.substrate.MS;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2016/12/16.
 */

public class SmsHookClassLoader implements MS.ClassLoadHook {
    String TAG = "hooksms";
    private static SmsHookClassLoader smsHookClassLoader;

    public SmsHookClassLoader() {
        super();
    }

    public static SmsHookClassLoader getInstance(){
        if (smsHookClassLoader == null) {
            smsHookClassLoader = new SmsHookClassLoader();
        }
        return smsHookClassLoader;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void classLoaded(Class<?> SmsManager) {
        //code to modify the class when loaded
        Method sendTextMessage;
        try {
            sendTextMessage = SmsManager.getMethod("sendTextMessage",
                    new Class[]{String.class,String.class,String.class,PendingIntent.class,PendingIntent.class});

        } catch (NoSuchMethodException e) {
            sendTextMessage = null;
        }
        MS.hookMethod(SmsManager, sendTextMessage, new MS.MethodAlteration() {
            public Object invoked(Object _this,Object... _args) throws Throwable{
                Log.i(TAG,"SEND_SMS");
                Log.i(TAG,"destination:"+_args[0]);
                Log.i(TAG,"source:"+_args[1]);
                Log.i(TAG,"text:"+_args[2]);
                return invoke(_this, _args);
            }
        });

    }
}
